<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.orderNo" placeholder="请输入订单号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.mobile" placeholder="请输入手机号码"></el-input>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" type="primary" icon="upload2" @click="exportExcels" v-show="exportBtnVisible">导出</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" type="primary" v-show="addBtnVisible" icon="plus" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" v-show="queryBtnVisible" type="primary" icon="search" v-on:click="getLogs(true)">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="Logs" stripe border :height="winHeight-188" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column prop="id" align="center" label="ID" width="100">
      </el-table-column>
      <el-table-column prop="orderNo" align="center" label="订单号" min-width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="natureType" align="center" label="性质" width="120" :formatter="formatNatureType">
      </el-table-column>
      <el-table-column prop="type" align="center" label="类别" width="120" :formatter="formatType">
      </el-table-column>
      <el-table-column prop="name" align="center" label="姓名" width="130">
      </el-table-column>
      <el-table-column prop="carId" align="center" label="证件号" min-width="130" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="mobile" align="center" label="手机号码" min-width="130" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="email" align="center" label="电子邮件" min-width="130" show-overflow-tooltip>
      </el-table-column>
      <!-- <el-table-column label="操作" fixed='right' width="100">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->

      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="120px" :rules="addFormRules" ref="addForm">
        <el-row>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="订单号:" prop="userName">
              <el-input v-model="addForm.orderNo" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="性质:" prop="nickName">
              <el-input v-model="addForm.natureType" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="类别:" prop="mobile">
              <el-input v-model="addForm.type" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="姓名:" prop="email">
              <el-input v-model="addForm.name" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="证件号:" prop="mobile">
              <el-input v-model="addForm.carId" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="手机号码:" prop="email">
              <el-input v-model="addForm.mobile" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="电子邮件:" prop="mobile">
              <el-input v-model="addForm.email" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="证件类型:" prop="email">
              <el-input v-model="addForm.carType" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="职业类别:" prop="mobile">
              <el-input v-model="addForm.occupation" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="与投保人关系:" prop="email">
              <el-input v-model="addForm.relation" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="性别:" prop="mobile">
              <el-input v-model="addForm.sex" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="出生日期:" prop="email">
              <el-input v-model="addForm.birth" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="20" :sm="12" :md="12" :lg="12">
            <el-form-item label="备注:" prop="mobile">
              <el-input v-model="addForm.remark" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" align="center" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'

import commons from '../../common/js/common'
//import NProgress from 'nprogress'
import { mapState, mapActions } from 'vuex'
import { policyInfo, addPolicy } from '../../api/api';

export default {
  data() {
    return {
      filters: {
        orderNo: '',
        name: '',
        mobile: ''
      },
      Logs: [],
      lists: [],
      exportTotal: 0,
      total: 0,
      page: 1,
      pageSize: 10,
      loading: '',
      // fullscreenLoading:false,
      listLoading: false,
      addLoading: false,
      addFormVisible: false,
      sels: [],//列表选中列
      userid: '',
      addForm: {
        birth: '',
        carId: '',
        carType: '',
        email: '',
        mobile: '',
        name: '',
        natureType: '',
        occupation: '',
        orderNo: '',
        relation: '',
        remark: '',
        sex: '',
        type: ''
      },
      addFormRules: {

      },
      IsShow: false,

      queryBtnVisible: false,
      addBtnVisible: false,
      exportBtnVisible: false
    }
  },
  computed: mapState([
    'winWidth',
    'winHeight'
  ]),
  methods: {
    cellStyle(row, rowIndex, columnIndex) {
    },
    formatNatureType: function (row, column) {
      return row.natureType == "1" ? "个人" : (row.natureType == "2" ? "企业" : "");
    },
    formatType: function (row, column) {
      return row.type == "1" ? "投保人" : (row.type == "2" ? "被保人" : (row.type == "3" ? "受益人" : ""));
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getLogs(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getLogs(true);
    },
    //获取列表
    getLogs(bool) {
      let para = new FormData();
      para.append("pageSize", bool ? this.pageSize : this.exportTotal);
      para.append("pageNumber", bool ? this.page : 1);
      para.append("name", this.filters.name);
      para.append("mobile", this.filters.mobile);
      para.append("orderNo", this.filters.orderNo);
      this.listLoading = bool;
      // this.fullscreenLoading = bool?false:true;
      if (!bool) {
        this.loading = this.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }
      policyInfo(para).then((res) => {
        this.lists = res.data.result.content;
        this.total = res.data.result.totalElements;
        this.exportTotal = res.data.result.totalElements;
        this.listLoading = false;
        if (!bool) {
          this.loading.close();
          this.export2Excel();
        }
        else {
          this.Logs = res.data.result.content;
        }
      });
    },
    //table导出excel
    exportExcels: function () {
      this.getLogs(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = ['ID', '订单号', '性质', '类别', '姓名', '证件号', '手机号码', '电子邮件'];
        const filterVal = ['id', 'orderNo', 'natureType', 'type', 'name', 'carId', 'mobil', 'email'];
        const list = this.lists;
        for (var i in list) {

          list[i].createTime = commons.getDaytime(list[i].createTime, 1, false);
          list[i].status = commons.payeeStatus(list[i].changeType);
        }
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '用户积分列表');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //显示新增界面
    handleAdd: function () {
      this.addFormVisible = true;
      this.addForm = {
        birth: '',
        carId: '',
        carType: '',
        email: '',
        mobile: '',
        name: '',
        natureType: '',
        occupation: '',
        orderNo: '',
        relation: '',
        remark: '',
        sex: '',
        type: ''
      };
    },
    addSubmit: function () {
      // this.$refs.addForm.validate((valid) => {
      // if (valid) {
      this.$confirm('确认提交吗？', '提示', {}).then(() => {
        this.addLoading = true;
        let para = Object.assign({}, this.addForm);
        addPolicy(para).then((res) => {
          if (res.data.success) {
            this.$message({
              message: '提交成功',
              type: 'success'
            });
            this.$refs['addForm'].resetFields();
            this.addFormVisible = false;
            this.getLogs(true);
            this.sels = [];
          }
          else {
            this.$message({
              message: res.data.result,
              type: 'error'
            });
          }
          this.addLoading = false;

        }).catch(() => {
          this.addLoading = false;
        });
      });
    },

    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('149'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "applicantQuery") {
          _this.queryBtnVisible = true;
        }
        if (thisData[i] == "applicantAdd") {
          _this.addBtnVisible = true;
        }
        if (thisData[i] == "applicantExport") {
          _this.exportBtnVisible = true;
        }
      });
    }
  },
  mounted() {
    this.getLogs(true);
    this.btnShow();
  }
}

</script>

<style scoped>

</style>